package com.dlc.felear.xiaoensale.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.itdlc.android.library.base.BaseActivity;

import org.apache.commons.lang3.StringEscapeUtils;
import org.simple.eventbus.EventBus;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 设备参数设置
 */
public class CookingMenParamersSettingActivity extends BaseActivity {
    private static final String TAG = "CookingMenParamers";
    private TopbarHelper topbarHelper;
    @BindView(R.id.ll1)
    View ll1;
    @BindView(R.id.ll2)
    View ll2;
    @BindView(R.id.dsp_et_a)
    EditText a;
    @BindView(R.id.dsp_et_m)
    EditText m;
    @BindView(R.id.dsp_et_m1)
    EditText b1;
    @BindView(R.id.dsp_et_m2)
    EditText b2;
    @BindView(R.id.dsp_et_z)
    EditText z1;
    @BindView(R.id.dsp_et_z2)
    EditText z2;
    @BindView(R.id.dsp_et_a1)
    EditText a1;
    @BindView(R.id.dsp_et_a12)
    EditText a2;
    @BindView(R.id.dsp_et_x21)
    EditText c1;
    @BindView(R.id.dsp_et_x22)
    EditText c2;
    @BindView(R.id.dsp_et_f)
    EditText f;
    @BindView(R.id.dsp_et_x1)
    EditText x;
    private int cookingType = 1;

    @Override
    protected int getLayoutRes() {
        EventBus.getDefault().register(this);
        return R.layout.activity_cooking_paramers_setting;
    }

    @Override
    protected void initialView() {
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setNavActivity(ParamerSettingActivity.class);
        topbarHelper.setcon(R.drawable.arrow_left);
        topbarHelper.setReturnEnable(false);
        topbarHelper.setNotReturn("不能点此返回");
        topbarHelper.setTitle("菜谱参数设置");
    }

    @Override
    protected void initData() {
        try {
            cookingType = getIntent().getIntExtra("cookingType", 0);
            this.cookingType = cookingType;
            int code = getIntent().getIntExtra("code", 0);
            String paramer1 = getIntent().getStringExtra("paramer1");
            initShowparamer(paramer1);
            Log.e(TAG, "cookingType=" + cookingType + " code=" + code + " paramer1:" + paramer1);
            if (cookingType == 1) {
                ll1.setVisibility(View.VISIBLE);
                ll2.setVisibility(View.GONE);
            }
            if (cookingType == 2) {
                ll2.setVisibility(View.VISIBLE);
                ll1.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            Log.e(TAG,e.getMessage());
            showShortToast("数据解析异常，请查证");
        }
    }

    private void initShowparamer(String p1) {
        if (!TextUtils.isEmpty(p1)) {
            String str = StringEscapeUtils.unescapeJava(p1);
            HashMap<String, Object> hashMap1 = JsonHelper.parserJson(str, HashMap.class);
            Log.e(TAG,"hashMap1:"+JsonHelper.toJson(hashMap1));
            a.setText(getMapValue(hashMap1,"a") + "");
            m.setText(getMapValue(hashMap1,"n") + "");
            x.setText(getMapValue(hashMap1,"m") + "");
            a1.setText(getMapValue(hashMap1,"a1") + "");
            b1.setText(getMapValue(hashMap1,"b1") + "");
            c1.setText(getMapValue(hashMap1,"c1") + "");
            z1.setText(getMapValue(hashMap1,"z1") + "");

            a2.setText(getMapValue(hashMap1,"a2") + "");
            b2.setText(getMapValue(hashMap1,"b2") + "");
            c2.setText(getMapValue(hashMap1,"c2") + "");
            z2.setText(getMapValue(hashMap1,"z2") + "");
            f.setText(getMapValue(hashMap1,"f"));
        }
    }

    private String getMapValue(HashMap<String,Object> map,String key){
        String temp="-1";
        if (map.containsKey(key)) {
           temp=map.get(key).toString();
        }
        return temp;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @OnClick({R.id.dsp_btn_commit1})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dsp_btn_commit1:
                save();
                break;
        }
    }

    private void save() {
        try {
            HashMap<String, Object> hashMap1 = new HashMap<>();
            if(cookingType==1){
                hashMap1.put("a", getEtValue(a));
                hashMap1.put("n", getEtValue(m));
                hashMap1.put("m", getEtValue(x));
            }else {
                hashMap1.put("a1", getEtValue(a1));
                hashMap1.put("b1", getEtValue(b1));
                hashMap1.put("c1", getEtValue(c1));
                hashMap1.put("z1", getEtValue(z1));
                hashMap1.put("a2", getEtValue(a2));
                hashMap1.put("b2", getEtValue(b2));
                hashMap1.put("c2", getEtValue(c2));
                hashMap1.put("z2", getEtValue(z2));
                hashMap1.put("f", getEtValue(f));
            }
            Intent intent = new Intent();
            intent.putExtra("paramer1", JsonHelper.toJson(hashMap1));
            setResult(1027, intent);
            finish();
        }catch (Exception e){
            showShortToast("数据异常:"+e.getMessage());
        }
    }


    private String getEtValue(EditText et) {
        String value = et.getText().toString().trim();
        if(TextUtils.isEmpty(value)){
          value="0";
        }
        return value;
    }
}